home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / src / lib / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  7.6 KB  |  357 lines

  1. //------------------------------------------------------------------------
  2. // ^FILE: dump.c - debugging/dumping functions of the CmdLine library
  3. //
  4. // ^DESCRIPTION:
  5. //    If DEBUG_CMDLINE is #defined when this file is compiled, then
  6. //    the functions that print out debugging information for a CmdLine
  7. //    and a CmdArg are implemented.
  8. //
  9. // ^HISTORY:
  10. //    04/01/92    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  11. //
  12. //    03/01/93    Brad Appleton    <brad@ssd.csd.harris.com>
  13. //    - Added arg_sequence field to CmdArg
  14. //    - Added cmd_nargs_parsed field to CmdLine
  15. //    - Added cmd_description field to CmdLine
  16. //-^^---------------------------------------------------------------------
  17.  
  18. #include  "cmdline.h"
  19.  
  20. #ifdef DEBUG_CMDLINE
  21. # include  <iostream.h>
  22. # include  <string.h>
  23.  
  24. # include  "arglist.h"
  25. # include  "states.h"
  26. #endif
  27.  
  28.  
  29. #ifdef DEBUG_CMDLINE
  30.  
  31.    // Indent a line corresponding to a given indent level.
  32.    // The number of spaces to indent is 3x the indent level
  33.    //
  34. static ostream &
  35. indent(ostream & os, unsigned level)
  36. {
  37.    os.width(level * 3);
  38.    return  (os << "");
  39. }
  40.  
  41.  
  42.    // Dump the arg_syntax field of a CmdArg in a mnemonic format
  43.    //
  44. static ostream &
  45. dump_arg_syntax(ostream & os, unsigned  syntax)
  46. {
  47.    if (syntax & CmdArg::isREQ) {
  48.       os << "isREQ" ;
  49.    } else {
  50.       os << "isOPT" ;
  51.    }
  52.  
  53.    if (syntax & CmdArg::isVALREQ) {
  54.       os << "+isVALREQ" ;
  55.    }
  56.    if (syntax & CmdArg::isVALOPT) {
  57.       os << "+isVALOPT" ;
  58.    }
  59.    if (syntax & CmdArg::isVALSEP) {
  60.       os << "+isVALSEP" ;
  61.    }
  62.    if (syntax & CmdArg::isVALSTICKY) {
  63.       os << "+isVALSTICKY" ;
  64.    }
  65.    if (syntax & CmdArg::isLIST) {
  66.       os << "+isLIST" ;
  67.    }
  68.    if (syntax & CmdArg::isPOS) {
  69.       os << "+isPOS" ;
  70.    }
  71.    if (syntax & CmdArg::isHIDDEN) {
  72.       os << "+isHID" ;
  73.    }
  74.  
  75.    return  os;
  76. }
  77.  
  78.  
  79.    // Dump the arg_flags field of a CmdArg in a mnemonic format
  80. static ostream &
  81. dump_arg_flags(ostream & os, unsigned  flags)
  82. {
  83.    if (flags & CmdArg::GIVEN) {
  84.       os << "GIVEN" ;
  85.    } else {
  86.       os << "NOTGIVEN";
  87.    }
  88.    if (flags & CmdArg::VALGIVEN) {
  89.       os << "+VALGIVEN";
  90.    }
  91.    if (flags & CmdArg::OPTION) {
  92.       os << "+OPTION";
  93.    }
  94.    if (flags & CmdArg::KEYWORD) {
  95.       os << "+KEYWORD";
  96.    }
  97.    if (flags & CmdArg::POSITIONAL) {
  98.       os << "+POSITIONAL";
  99.    }
  100.    if (flags & CmdArg::VALSEP) {
  101.       os << "+VALSEP";
  102.    } else if (flags & CmdArg::VALGIVEN) {
  103.       os << "+VALSAME";
  104.    }
  105.  
  106.    return  os;
  107. }
  108.  
  109.  
  110.    // Dump the cmd_flags field of a CmdLine in a mnemonic format
  111. static ostream &
  112. dump_cmd_flags(ostream & os, unsigned flags)
  113. {
  114.    if (flags & CmdLine::NO_ABORT) {
  115.       os << "NO_ABORT" ;
  116.    } else {
  117.       os << "ABORT" ;
  118.    }
  119.    if (flags & CmdLine::ANY_CASE_OPTS) {
  120.       os << "+ANY_CASE_OPTS";
  121.    }
  122.    if (flags & CmdLine::PROMPT_USER) {
  123.       os << "+PROMPT_USER";
  124.    }
  125.    if (flags & CmdLine::OPTS_FIRST) {
  126.       os << "+OPTS_FIRST";
  127.    }
  128.    if (flags & CmdLine::OPTS_ONLY) {
  129.       os << "+OPTS_ONLY";
  130.    }
  131.    if (flags & CmdLine::KWDS_ONLY) {
  132.       os << "+KWDS_ONLY";
  133.    }
  134.    if (flags & CmdLine::TEMP) {
  135.       os << "+TEMP";
  136.    }
  137.    if (flags & CmdLine::QUIET) {
  138.       os << "+QUIET";
  139.    }
  140.    if (flags & CmdLine::NO_GUESSING) {
  141.       os << "+NO_GUESSING";
  142.    }
  143.  
  144.    return  os;
  145. }
  146.  
  147.  
  148.    // Dump the status of a CmdLine in a mnemonic format
  149. static ostream &
  150. dump_cmd_status(ostream & os, unsigned  status)
  151. {
  152.    if (! status) {
  153.       os << "NO_ERROR";
  154.       return  os;
  155.    } else {
  156.       os << "ERROR";
  157.    }
  158.    if (status & CmdLine::ARG_MISSING) {
  159.       os << "+ARG_MISSING";
  160.    }
  161.    if (status & CmdLine::VAL_MISSING) {
  162.       os << "+VAL_MISSING";
  163.    }
  164.    if (status & CmdLine::VAL_NOTSTICKY) {
  165.       os << "+VAL_NOTSTICKY";
  166.    }
  167.    if (status & CmdLine::VAL_NOTSEP) {
  168.       os << "+VAL_NOTSEP";
  169.    }
  170.    if (status & CmdLine::KWD_AMBIGUOUS) {
  171.       os << "+KWD_AMBIG";
  172.    }
  173.    if (status & CmdLine::BAD_OPTION) {
  174.       os << "+BAD_OPT";
  175.    }
  176.    if (status & CmdLine::BAD_KEYWORD) {
  177.       os << "+BAD_KWD";
  178.    }
  179.    if (status & CmdLine::BAD_VALUE) {
  180.       os << "+BAD_VAL";
  181.    }
  182.    if (status & CmdLine::TOO_MANY_ARGS) {
  183.       os << "+TOO_MANY";
  184.    }
  185.  
  186.    return  os;
  187. }
  188.  
  189.  
  190.    // Dump the state of a CmdLine in a mnemonic format
  191. static ostream &
  192. dump_cmd_state(ostream & os, unsigned  state)
  193. {
  194.    if (! state) {
  195.       os << "NO_OPTIONS";
  196.    } else {
  197.       os << "ARGS";
  198.    }
  199.    if (state & cmd_END_OF_OPTIONS) {
  200.       os << "+ENDOPTS";
  201.    }
  202.    if (state & cmd_OPTIONS_USED) {
  203.       os << "+OPTS_USED";
  204.    }
  205.    if (state & cmd_KEYWORDS_USED) {
  206.       os << "+KWDS_USED";
  207.    }
  208.    if (state & cmd_GUESSING) {
  209.       os << "+GUESSING";
  210.    }
  211.  
  212.    return  os;
  213. }
  214.  
  215.  
  216.    // Dump the parse_state of a CmdLine in a mnemonic format
  217. static ostream &
  218. dump_cmd_parse_state(ostream & os, unsigned parse_state)
  219. {
  220.    switch (parse_state) {
  221.    case cmd_START_STATE :
  222.       os << "START_STATE";
  223.       break;
  224.  
  225.    case cmd_TOK_REQUIRED :
  226.       os << "TOK_REQUIRED";
  227.       break;
  228.  
  229.    case cmd_WANT_VAL :
  230.       os << "WANT_VAL";
  231.       break;
  232.  
  233.    case cmd_NEED_VAL :
  234.       os << "NEED_VAL";
  235.       break;
  236.  
  237.  
  238. #ifdef vms_style
  239.    case cmd_WANT_VALSEP :
  240.       os << "WANT_VALSEP";
  241.       break;
  242.  
  243.    case cmd_NEED_VALSEP :
  244.       os << "NEED_VALSEP";
  245.       break;
  246.  
  247.    case cmd_WANT_LISTSEP :
  248.       os << "WANT_LISTSEP";
  249.       break;
  250.  
  251.    case cmd_NEED_LISTSEP :
  252.       os << "NEED_LISTSEP";
  253.       break;
  254.  
  255. #endif
  256.  
  257.    default :
  258.       os << parse_state;
  259.    }
  260.  
  261.    return  os;
  262. }
  263.  
  264.  
  265.    // Dump the arguments (including the default arguments) in an arg_list
  266. static ostream &
  267. dump_cmd_args(ostream & os, CmdArgListList * arg_list, unsigned level)
  268. {
  269.    ::indent(os, level) << "CmdLine::cmd_args {\n" ;
  270.  
  271.    CmdArgListListIter  list_iter(arg_list);
  272.    for (CmdArgList * alist = list_iter() ; alist ; alist = list_iter()) {
  273.       CmdArgListIter iter(alist);
  274.       for (CmdArg * cmdarg = iter() ; cmdarg ; cmdarg = iter()) {
  275.          cmdarg->dump(os, level + 1);
  276.       }
  277.    }
  278.  
  279.    ::indent(os, level) << "}" << endl;
  280.    return  os;
  281. }
  282.  
  283. #endif  /* DEBUG_CMDLINE */
  284.  
  285.  
  286.    // Dump a CmdArg
  287. void
  288. CmdArg::dump(ostream & os, unsigned level) const
  289. {
  290. #ifdef DEBUG_CMDLINE
  291.    ::indent(os, level) << "CmdArg {\n" ;
  292.  
  293.    ::indent(os, level + 1) << "option='" << char(arg_char_name) << "', "
  294.                          << "keyword=\"" << arg_keyword_name << "\", "
  295.                          << "value=\"" << arg_value_name << "\"\n" ;
  296.  
  297.    ::indent(os, level + 1) << "syntax=" ;
  298.    dump_arg_syntax(os, arg_syntax) << "\n";
  299.  
  300.    ::indent(os, level + 1) << "flags=" ;
  301.    dump_arg_flags(os, arg_flags) << "\n";
  302.  
  303.    ::indent(os, level + 1) << "sequence=" << arg_sequence << "\n";
  304.  
  305.    ::indent(os, level) << "}" << endl;
  306. #endif
  307. }
  308.  
  309.  
  310.    // Dump a CmdLine
  311. void
  312. CmdLine::dump(ostream & os, unsigned level) const
  313. {
  314. #ifdef DEBUG_CMDLINE
  315.    ::indent(os, level) << "CmdLine {\n" ;
  316.  
  317.    ::indent(os, level + 1) << "name=\"" << cmd_name << "\"\n";
  318.  
  319.    ::indent(os, level + 1) << "description=\"" << cmd_description << "\"\n";
  320.  
  321.    ::indent(os, level + 1) << "flags=" ;
  322.    dump_cmd_flags(os, cmd_flags) << "\n";
  323.  
  324.    ::indent(os, level + 1) << "status=" ;
  325.    dump_cmd_status(os, cmd_status) << "\n";
  326.  
  327.    ::indent(os, level + 1) << "state=" ;
  328.    dump_cmd_state(os, cmd_state) << "\n";
  329.  
  330.    ::indent(os, level + 1) << "parse_state=" ;
  331.    dump_cmd_parse_state(os, cmd_parse_state) << "\n";
  332.  
  333.    ::indent(os, level + 1);
  334.    if (cmd_matched_arg == NULL) {
  335.       os << "matched_arg=NULL\n";
  336.    } else {
  337.       os << "matched_arg=" << (void *)cmd_matched_arg << "\n";
  338.    }
  339.  
  340.    ::indent(os, level + 1) << "# valid-args-parsed="
  341.                            << cmd_nargs_parsed << "\n" ;
  342.  
  343.    ::indent(os, level) << "}" << endl;
  344. #endif
  345. }
  346.  
  347.  
  348.    // Dump the arguments of a CmdLine
  349. void
  350. CmdLine::dump_args(ostream & os, unsigned level) const
  351. {
  352. #ifdef DEBUG_CMDLINE
  353.    dump_cmd_args(os, cmd_args, level);
  354. #endif
  355. }
  356.  
  357.